package cn.falcon.fmobi.console.requestparam;

import cn.falcon.fmobi.dao.po.Strategy;
import cn.falcon.fmobi.dao.po.TargetInfo;
import cn.falcon.fmobi.dao.util.DataUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: shirongkang
 * Date: 2017/10/16
 * Time: 16:11
 * Desc:
 */
public class StrategyParam extends Strategy {

    private String targetName;

    private TargetParam targetParam;

    public TargetParam getTargetParam() {
        return targetParam;
    }

    public void setTargetParam(TargetParam targetParam) {
        this.targetParam = targetParam;
    }

    public String getTargetName() {
        return targetName;
    }

    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }

    public Strategy getStrategy() throws ParseException {
        Strategy strategy = new Strategy();
        strategy.setId(this.getId());
        strategy.setName(this.getName());
        strategy.setDescription(this.getDescription());
        strategy.setType(this.getType());
        strategy.setStartTime(this.getStartTime());
        if(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.getEndTime()).equals("9999-12-31 23:59:59")) {
            strategy.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2027-12-01 23:59:59"));
        }else {
            strategy.setEndTime(this.getEndTime());
        }
        strategy.setSpaceId(this.getSpaceId());
        strategy.setCreativeId(this.getCreativeId());
        strategy.setCampaignId(this.getCampaignId());
        strategy.setPromotionPlanId(this.getPromotionPlanId());
        strategy.setTargetPackageId(this.getTargetPackageId());
        strategy.setUnitPrice(this.getUnitPrice());
        strategy.setFeeType(this.getFeeType());
        strategy.setDayBudget(this.getDayBudget());
        return strategy;
    }

    /**
     * 定向信息
     * @return
     */
    public TargetInfo getTargetInfo() {
        TargetInfo targetInfo = new TargetInfo();
        targetInfo.setName(this.getTargetName());
        targetInfo.setAge(this.getTargetParam().getAge());
        targetInfo.setType(this.getTargetParam().getType());
        if (null != this.getTargetParam().getBehavior()) {
            targetInfo.setAppBehavior(DataUtils.toJSONString(this.getTargetParam().getBehavior()));
        }else {
            targetInfo.setAppBehavior("");
        }
        if (null != this.getTargetParam().getCustomAudience()) {
            targetInfo.setCustomAudience(StringUtils.join(this.getTargetParam().getCustomAudience(), ","));
        }else {
            targetInfo.setCustomAudience("");
        }
        if (null != this.getTargetParam().getEducation()) {
            targetInfo.setEducation(StringUtils.join(this.getTargetParam().getEducation(), ","));
        }else {
            targetInfo.setEducation("");
        }
        if (null != this.getTargetParam().getGender()) {
            targetInfo.setGender(StringUtils.join(this.getTargetParam().getGender(), ","));
        }else {
            targetInfo.setGender("");
        }
        if (null != this.getTargetParam().getMarriage()) {
            targetInfo.setRelationshipStatus(StringUtils.join(this.getTargetParam().getMarriage(), ","));
        }else {
            targetInfo.setRelationshipStatus("");
        }
        if (null != this.getTargetParam().getArea()) {
            targetInfo.setGeoLocation(DataUtils.toJSONString(this.getTargetParam().getArea()));
        }else {
            targetInfo.setGeoLocation("");
        }
        targetInfo.setKeyword(DataUtils.isNull(this.getTargetParam().getKeyword())? "" : this.getTargetParam().getKeyword());
        if (null != this.getTargetParam().getTelcom()) {
            targetInfo.setNetworkOperator(StringUtils.join(this.getTargetParam().getTelcom(), ","));
        }else {
            targetInfo.setNetworkOperator("");
        }
        if (null != this.getTargetParam().getConnection()) {
            targetInfo.setNetworkType(StringUtils.join(this.getTargetParam().getConnection(), ","));
        }else {
            targetInfo.setNetworkType("");
        }
        if (null != this.getTargetParam().getHours()) {
            targetInfo.setTimeWindow(DataUtils.toJSONString(this.getTargetParam().getHours()));
        }else {
            targetInfo.setTimeWindow("");
        }
        if (null != this.getTargetParam().getOs()) {
            targetInfo.setUserOs(StringUtils.join(this.getTargetParam().getOs(), ","));
        }else {
            targetInfo.setUserOs("");
        }
        if (!DataUtils.isNull(this.getTargetParam().getInterests())) {
            targetInfo.setBusinessInterest(StringUtils.join(this.getTargetParam().getInterests(), ","));
        }else {
            targetInfo.setBusinessInterest("");
        }
        return targetInfo;
    }
}
